package com.push.web.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.push.common.annotation.Log;
import com.push.common.core.controller.BaseController;
import com.push.common.core.domain.AjaxResult;
import com.push.common.enums.BusinessType;
import com.push.web.domain.BizLabelFirst;
import com.push.web.service.IBizLabelFirstService;
import com.push.common.utils.poi.ExcelUtil;
import com.push.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 一级分类Controller
 *
 * @author yanxi
 * @date 2022-03-09
 */
@RestController
@RequestMapping("/information/labelFirst")
public class BizLabelFirstController extends BaseController
{
    @Autowired
    private IBizLabelFirstService bizLabelFirstService;

    /**
     * 查询一级分类列表
     */
    @PreAuthorize("@ss.hasPermi('information:label:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizLabelFirst bizLabelFirst)
    {
        startPage();
        List<BizLabelFirst> list = bizLabelFirstService.selectBizLabelFirstList(bizLabelFirst);
        return getDataTable(list);
    }

    /**
     * 导出一级分类列表
     */
    @PreAuthorize("@ss.hasPermi('information:label:export')")
    @Log(title = "一级分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizLabelFirst bizLabelFirst)
    {
        List<BizLabelFirst> list = bizLabelFirstService.selectBizLabelFirstList(bizLabelFirst);
        ExcelUtil<BizLabelFirst> util = new ExcelUtil<BizLabelFirst>(BizLabelFirst.class);
        return util.exportExcel(list, "一级分类数据");
    }

    /**
     * 获取一级分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('information:label:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizLabelFirstService.selectBizLabelFirstById(id));
    }

    /**
     * 新增一级分类
     */
    @PreAuthorize("@ss.hasPermi('information:label:add')")
    @Log(title = "一级分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizLabelFirst bizLabelFirst)
    {
        return toAjax(bizLabelFirstService.insertBizLabelFirst(bizLabelFirst));
    }

    /**
     * 修改一级分类
     */
    @PreAuthorize("@ss.hasPermi('information:label:edit')")
    @Log(title = "一级分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizLabelFirst bizLabelFirst)
    {
        return toAjax(bizLabelFirstService.updateBizLabelFirst(bizLabelFirst));
    }

    /**
     * 删除一级分类
     */
    @PreAuthorize("@ss.hasPermi('information:label:remove')")
    @Log(title = "一级分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizLabelFirstService.deleteBizLabelFirstByIds(ids));
    }

    /**
     * 批量上传
     */
    @RequestMapping("/importData")
    @ResponseBody
    public AjaxResult importData(@RequestParam MultipartFile file, @RequestParam boolean updateSupport) throws Exception {
        ExcelUtil<BizLabelFirst> util = new ExcelUtil<BizLabelFirst>(BizLabelFirst.class);
        List<BizLabelFirst> labels = util.importExcel(file.getInputStream());
        //循环插入数据
        for (BizLabelFirst label:labels) {
            List<BizLabelFirst> result = bizLabelFirstService.selectBizLabelFirstList(label);
            if(result.size() > 0) {
                //如果设置了更新
                if(updateSupport) {
                    bizLabelFirstService.updateBizLabelFirst(label);
                }
            } else {
                //之前不存在直接插入
                bizLabelFirstService.insertBizLabelFirst(label);
            }
        }
        return AjaxResult.success("导入成功");
    }
}
